package com.bdqn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * SysUserServiceImpl
 *
 * @author LILIBO
 * @since 2025/3/19
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 根据姓名和角色ID查找用户列表（自定义接口）
     *
     * @param usrName
     * @param usrRoleId
     * @return
     */
    @Override
    public List<SysUser> getByUsrNameAndRoleId(String usrName, Long usrRoleId) {
        return sysUserMapper.getByUsrNameAndRoleId(usrName, usrRoleId);
    }

    /**
     * 根据姓名和密码查找用户信息（自定义接口）
     *
     * @param usrName
     * @param usrPassword
     * @return
     */
    @Override
    public SysUser getByUsrNameAndUsrPassword(String usrName, String usrPassword) {
        return sysUserMapper.getByUsrNameAndUsrPassword(usrName, usrPassword);
    }

    /**
     * 登录
     *
     * @param usrName
     * @param usrPassword
     * @return
     */
    @Override
    public SysUser login(String usrName, String usrPassword) {
        if (usrName == null || usrPassword == null) {
            return null;
        }

        // 使用LambdaQueryWrapper避免写死字段名
        /* LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper<>();
        qw.eq(SysUser :: getUsrName, usrName);
        SysUser sysUser = baseMapper.selectOne(qw); */
        // 调用Mapper层方法查询用户信息
        SysUser sysUser = sysUserMapper.getByAccount(usrName);
        // 判断查询结果是否为空，或者密码是否一致
        if (sysUser == null || !usrPassword.equals(sysUser.getUsrPassword())) {
            return null;
        }
        // 登录成功，返回查询结果
        return sysUser;
    }

    /**
     * 分页查询用户列表
     *
     * @param usrName
     * @param usrRoleId
     * @param page
     * @return
     * @Cacheable 首先查缓存有就返回，没有就执行方法，方法返回不为空添加缓存
     * value: 缓存名称，可以多个
     * key: 缓存的键值
     * keyGenerator = "keyGenerator": 缓存的key自动生成
     * condition: 满足条件的调用，结果才会被缓存（不指定时相当于true-缓存）
     * unless: 不合适的返回，结果不缓存（不指定时相当于false-缓存）
     * 总结: 当condition=false一定不缓存；当condition=true且unless=true不缓存；当condition=true且unless=false缓存
     */
    @Override
    @Cacheable(value = "sysuser", key = "'page:' + #page.current + '-' + #page.size + '-' + #usrName + '-' + #roleId", condition = "#page.current!=null and #page.size!=null", unless = "#result==null")
    public Page<SysUser> queryPage(String usrName, Long usrRoleId, Page<SysUser> page) {
        return sysUserMapper.queryPage(usrName, usrRoleId, page);
    }

    /**
     * 检查用户名是否存在（排除本身ID）
     *
     * @param usrName
     * @param usrId
     * @return
     */
    @Override
    public String check(String usrName, Long usrId) {
        if (usrName == null || usrName.isEmpty()) {
            return "error";
        }
        // 使用LambdaQueryWrapper避免写死字段名
        LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper<>();
        qw.eq(SysUser :: getUsrName, usrName);
        if (usrId != null) {
            qw.ne(SysUser :: getUsrId, usrId);
        }
        SysUser sysUser = baseMapper.selectOne(qw);

        return sysUser != null ? "false" : "true";
    }

    /**
     * 保存用户（兼容新增和修改）
     *
     * @param sysUser 实体对象
     * @return
     * @CachePut 首先执行方法体，执行完毕后根据参数设置进行缓存覆盖
     * value: 缓存名称
     * key: 缓存的键值
     * condition: 满足条件的调用，结果才会被缓存（不指定时相当于true-缓存）
     * unless: 不合适的返回，结果不缓存（不指定时相当于false-缓存）
     * 总结: 当condition=false一定不缓存；当condition=true且unless=true不缓存；当condition=true且unless=false缓存
     */
    @Override
    @CachePut(value = "sysuser", key = "'usrId:' + #sysUser.usrId", condition = "#sysUser!=null", unless = "#result==null")
    @CacheEvict(value = "sysuser", allEntries = true)
    public SysUser saveSysUser(SysUser sysUser) {
        return super.saveOrUpdate(sysUser) ? sysUser : null;
    }

    /**
     * 删除用户
     *
     * @param usrId
     * @return
     */
    @Override
    @CacheEvict(value = "sysuser", allEntries = true)
    public Boolean removeSysUser(Long usrId) {
        return super.removeById(usrId);
    }

}
